//
//  CalendarLogic1.m
//  Calendar
//
//  Created by 张凡 on 14-7-3.
//  Copyright (c) 2014年 张凡. All rights reserved.
//

#import "CalendarLogic.h"
#import "ResponseDayModel.h"

@interface CalendarLogic (){
    CalendarDayModel *selectcalendarDay;
}


@property (nonatomic , strong) NSMutableDictionary *dicPriceSource;

@end

@implementation CalendarLogic

-(NSMutableArray *)setResponseDayModels:(NSArray *)responseDayModels{
    
    [self initDicPriceSource:responseDayModels];
    
    NSMutableArray *months = [self getMonths:responseDayModels];
    
  
    
    NSMutableArray *calendarMonth = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < months.count; i ++) {
        
        NSMutableArray *calendarDays = [[NSMutableArray alloc]init];
        
        NSDate *month = [self getDateFromString:[months objectAtIndex:i] format:@"yyyy-MM-dd"];

        [self calculateDaysInPreviousMonthWithDate:month andArray:calendarDays];
        
        [self calculateDaysInCurrentMonthWithDate:month andArray:calendarDays];
        
        [self calculateDaysInFollowingMonthWithDate:month andArray:calendarDays];
        
        [calendarMonth insertObject:calendarDays atIndex:i];
        
    }
    return calendarMonth;
}

-(void)initDicPriceSource:(NSArray *)responseDayModels{
    self.dicPriceSource = [[NSMutableDictionary alloc ] init];
    for (ResponseDayModel *dayModel in responseDayModels) {
        [self.dicPriceSource setObject:dayModel forKey:dayModel.time];
    }
}

-(NSDate *)getDateFromString:(NSString *)dateString format:(NSString *)format{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [NSTimeZone resetSystemTimeZone];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter setDateFormat:format];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    return [dateFormatter dateFromString:dateString];
}

/**
 *  获取总共月份
 *
 *  @param arrStr 格式 "yyy-MM-dd" 的数组
 *
 *  @return 月份的天数的总和
 */
-(NSMutableArray *)getMonths:(NSArray *)arrStr
{
    NSMutableArray *tempList = [NSMutableArray arrayWithArray:arrStr];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (ResponseDayModel *dayModel in tempList) {
        NSString *yAm = [dayModel.time substringToIndex:7];
        [dict setObject:dayModel.time forKey:yAm];
    }
    
    NSMutableArray *months = [[NSMutableArray alloc] init];
    for (NSString *value in dict.allValues) {
        [months addObject:value];
    }
    
  
    
    [months sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
        
    }];

    return months;
}

#pragma mark - 日历上+当前+下月份的天数

//计算上月份的天数
- (NSMutableArray *)calculateDaysInPreviousMonthWithDate:(NSDate *)date andArray:(NSMutableArray *)array
{
    NSUInteger weeklyOrdinality = [[date firstDayOfCurrentMonth] weeklyOrdinality];//计算这个的第一天是礼拜几,并转为int型
    NSDate *dayInThePreviousMonth = [date dayInThePreviousMonth];//上一个月的NSDate对象
    NSUInteger daysCount = [dayInThePreviousMonth numberOfDaysInCurrentMonth];//计算上个月有多少天
    NSUInteger partialDaysCount = weeklyOrdinality - 1;//获取上月在这个月的日历上显示的天数
    NSDateComponents *components = [dayInThePreviousMonth YMDComponents];//获取年月日对象
    
    for (NSInteger i = daysCount - partialDaysCount + 1; i < daysCount + 1; ++i) {
        CalendarDayModel *calendarDay = [CalendarDayModel calendarDayWithYear:components.year month:components.month day:i];
        calendarDay.style = CellDayTypeEmpty;//不显示
        [array addObject:calendarDay];
    }
    return NULL;
}

//计算下月份的天数
- (void)calculateDaysInFollowingMonthWithDate:(NSDate *)date andArray:(NSMutableArray *)array
{
    NSUInteger weeklyOrdinality = [[date lastDayOfCurrentMonth] weeklyOrdinality];
    if (weeklyOrdinality == 7) return ;
    
    NSUInteger partialDaysCount = 7 - weeklyOrdinality;
    NSDateComponents *components = [[date dayInTheFollowingMonth] YMDComponents];
    
    for (NSInteger i = 1; i < partialDaysCount + 1; ++i) {
        CalendarDayModel *calendarDay = [CalendarDayModel calendarDayWithYear:components.year month:components.month day:i];
        calendarDay.style = CellDayTypeEmpty;
        [array addObject:calendarDay];
    }
}

//计算当月的天数
- (void)calculateDaysInCurrentMonthWithDate:(NSDate *)date andArray:(NSMutableArray *)array
{
    NSUInteger daysCount = [date numberOfDaysInCurrentMonth];//计算这个月有多少天
    NSDateComponents *components = [date YMDComponents];//今天日期的年月日
    for (int i = 1; i < daysCount + 1; ++i) {
        CalendarDayModel *calendarDay = [CalendarDayModel calendarDayWithYear:components.year month:components.month day:i];
        calendarDay.week = [[calendarDay date]getWeekIntValueWithDate];
        
        NSString *priceKey = [NSString stringWithFormat:@"%ld-%.2ld-%.2d",(long)components.year,components.month,i];
        
        ResponseDayModel *dayModel = [self.dicPriceSource objectForKey:priceKey];
        calendarDay.price = dayModel.price;
        calendarDay.ticketStyle = dayModel.ticketStyle;
        calendarDay.SalePrice  = dayModel.SalePrice;
        calendarDay.BCID    = dayModel.BCID;
        calendarDay.workDate = dayModel.time;
        calendarDay.DetailId = dayModel.DetailId;
        [self changStyle:calendarDay];
        [array addObject:calendarDay];
    }
}

- (void)changStyle:(CalendarDayModel *)calendarDay{
    if (calendarDay.price == nil) {
        calendarDay.style = CellDayTypeEmptyPrice;
    }else{
        calendarDay.style = CellDayTypeShowPrice;
    }
}

- (void)selectLogic:(CalendarDayModel *)day{
    if (day.style == CellDayTypeClick) {
        day.style = CellDayTypeShowPrice;
    }else{
        day.style = CellDayTypeClick;
    }
}

@end
